package one.stand.controller;

import com.github.pagehelper.PageHelper;
import com.jmp.api.admin.dto.*;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import one.stand.dto.ConvertUtils;
import one.stand.dto.PageResponse2;
import one.stand.mapper.ArticleApiAdminPcMapper;
import one.stand.model.ArticleType;
import one.stand.model.ResultModel;
import one.stand.service.sys.ArticleTypeService;
import one.stand.util.SqlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author cjr
 */
@Api(description = "文章分类")
@RestController
@RequestMapping("adminApi/article/type")
@Slf4j
public class ArticleTypeController {

    @Autowired
    private ArticleApiAdminPcMapper articleApiAdminPcMapper;
    @Autowired
    private ArticleTypeService articleTypeService;

    @ApiOperation(value = "列表")
    @PostMapping("")
    @SecurityAuthCheck
    public ResultModel<ArticleTypeListResponse> list(@RequestBody ArticleTypeListRequest request) {

        String nameLike = SqlUtil.like(request.getNameLike());
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<ArticleType> articleTypeList = articleApiAdminPcMapper.selectType(nameLike);
        PageResponse2<ArticleTypeListResponse.Type> types = ConvertUtils.page2(articleTypeList, articleType -> ArticleTypeListResponse.Type.builder()
                .id(articleType.getId())
                .name(articleType.getName())
                .count(articleTypeService.count(articleType.getId()))
                .build());

        ArticleTypeListResponse response = ArticleTypeListResponse.builder()
                .types(types)
                .build();
        return ResultModel.success(response);
    }

    @ApiOperation(value = "保存")
    @PostMapping("save")
    @SecurityAuthCheck
    public ResultModel save(@RequestBody ArticleTypeSaveRequest request) {

        ArticleType articleType = new ArticleType();
        articleType.setId(request.getId());
        articleType.setName(request.getName());
        articleTypeService.save(articleType);

        return ResultModel.success();
    }


    @ApiOperation(value = "删除")
    @PostMapping("delete")
    @SecurityAuthCheck
    public ResultModel delete(@RequestBody ArticleTypeDeleteRequest request) {

        articleTypeService.delete(request.getId());

        return ResultModel.success();
    }

    @ApiOperation(value = "选择列表")
    @PostMapping("select")
    @SecurityAuthCheck
    public ResultModel<ArticleTypeSelectResponse> select() {

        List<ArticleType> articleTypeList = articleApiAdminPcMapper.selectType(null);
        List<ArticleTypeSelectResponse.Type> types = ConvertUtils.list(articleTypeList, articleType -> ArticleTypeSelectResponse.Type.builder()
                .id(articleType.getId())
                .name(articleType.getName())
                .build());

        ArticleTypeSelectResponse response = ArticleTypeSelectResponse.builder()
                .types(types)
                .build();
        return ResultModel.success(response);
    }

}
